<template>
	<view class="my-content">
		<MyList :listData="list" :isPublish="true" @offCat="offCat"></MyList>
		<uni-load-more :status="status" v-if="total > 0"></uni-load-more>
	</view>
</template>

<script>
const app = getApp();
import MyList from '../components/my-list/my-list.vue';
import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
import { myPost, offCat } from '@/api/index.js';
import { formatTime } from '@/util/time.js';
export default {
	data() {
		return {
			openid: '',
			page: 1,
			total: 1,
			list: [],
			status: 'more'
		};
	},
	methods: {
		myPost: function() {
			this.status = 'loading';
			myPost({
				page: this.page,
				openid: this.openid
			}).then(res => {
				if (res.pagenum == res.total || res.total == 0) {
					this.status = 'noMore';
				} else {
					this.status = 'more';
				}
				res.list.forEach(obj => {
					obj.birth = formatTime(obj.birth);
				});
				this.list = this.list.concat(res.list);
				this.total = res.total;
			});
		},
		// 下架
		offCat(id) {
			uni.showModal({
				title: '提示',
				content: '你确定要下架么？',
				success: res => {
					if (res.confirm) {
						offCat({
							id: id,
							openid: this.openid
						}).then(res => {
							uni.showToast({
								title: '下架成功',
								duration: 2000
							});
							this.page = 1;
							this.list = [];
							this.myPost();
						});
					}
				}
			});
		}
	},

	// 上拉加载
	onReachBottom() {
		if (this.page === this.total) {
			return false;
		}
		this.page += 1;
		this.myPost();
	},

	onLoad() {
		this.openid = uni.getStorageSync('openid');
		this.myPost();
	},
	components: {
		MyList,
		uniLoadMore
	}
};
</script>

<style lang="scss" scoped></style>
